Passed
Push — feature/full-redesign ( 58f6d4...ff7e17 )
by Kevin Van
04:32
created

helper.ts ➔ mapPsdStatus   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 10
rs 9.95
c 0
b 0
f 0
cc 1
1
export function mapMatchStatus(statusCode) {
2
  const statusCodes = new Map([
3
    [`PP`, `Uitgesteld`],
4
    [`ST`, `Stopgezet`],
5
    [`F1`, `Forfait`],
6
    [`FI`, `Forfait`],
7
    [`F2`, `Forfait`],
8
    [`FF`, `Forfait`],
9
    [`AMC`, `Algemeen forfait`],
10
  ])
11
12
  return statusCodes.get(statusCode) || null
13
}
14
15
/**
16
 * Strip first character if the division matches a youth division structure AND starts with a number.
17
 *
18
 * General form:
19
 *  - G9E
20
 *  - 2G10G
21
 *  - P7L
22
 *  - 2P12M
23
 * The function should return true if it matches the form starting with a "2".
24
 *
25
 * @param {string} division
26
 */
27
export function isYouthDivisionWithNumericFirst(division) {
28
  return /^(\d+)([a-zA-Z]+)(\d*)([a-zA-Z])/.test(division)
29
}
30
31
/**
32
 * Remove the first character of a division string if it's a number.
33
 *
34
 * Numbers are sometimes added for youth divisions to indicate a second period within a same season.
35
 * For example, G9K is the regional U9 division K before new year's day. From January 1st, the teams
36
 * can be re-arranged in a new (more balanced) division, which will be named something like 2G2K,
37
 * with the "2" in front indicating this difference.
38
 *
39
 * @param {string} division
40
 */
41
export function replaceFirstCharIfNumber(division) {
42
  if (isYouthDivisionWithNumericFirst(division)) {
43
    // Remove first character.
44
    division = division.substr(1)
45
  }
46
47
  return division
48
}
49
50
/**
51
 * Convert a region+division into an output label.
52
 *
53
 * @param {array} divisionArray
54
 * @param {string} level
55
 */
56
export function outputDivision(divisionArray, level = ``) {
57
  if (divisionArray[0] === `BCA`) {
58
    return `Beker van Brabant`
59
  } else if (divisionArray[0] === `ESCA`) {
60
    return `Beker voor B-ploegen`
61
  } else if (divisionArray[0] === `FR`) {
62
    return `Vriendschappelijk`
63
  } else if (divisionArray[0] === `BVZ`) {
64
    return `Beker van Zemst`
65
  } else if (divisionArray[2] <= 4) {
66
    return `${divisionArray[2]}e ${level !== `nat` ? `Prov.` : `Nationale`} ${divisionArray[3]}`
67
  } else {
68
    return `U${divisionArray[2]} / ${divisionArray[3]}${divisionArray[4] ? ` / ${divisionArray[4]}` : ``}`
69
  }
70
}
71
72
/**
73
 * Replace a divisionCode with its descriptive label.
74
 *
75
 * @param {string} division
76
 */
77
export function mapDivision(division) {
78
  return /^([A-Z]+)?(\d+)?([a-zA-Z]+)(\d*)$/.exec(replaceFirstCharIfNumber(division))
79
}
80
81
/**
82
 * Retrieve mapping and the formatted descriptive label of a division.
83
 *
84
 * @param {string} division
85
 * @param {string} region
86
 */
87
export function formatDivision(division, region) {
88
  const divisionArr = mapDivision(division)
89
  return outputDivision(divisionArr, region)
90
}
91
92
/**
93
 * Truncate to <n> letters and optionally stop at the last word instead of letter.
94
 *
95
 * @param {int} size
96
 * @param {boolean} useWordBoundary
97
 */
98
export function truncate(size, useWordBoundary = true) {
99
  if (this.length <= size) {
100
    return this
101
  }
102
  const subString = this.substr(0, size - 1)
103
  return (useWordBoundary ? subString.substr(0, subString.lastIndexOf(` `)) : subString) + `…`
104
}
105
106
107
export function mapPsdStatusIcon(statusCode) {
108
  const statusCodes = new Map([
109
    [0, ``],
110
    [1, `fa-times`],
111
    [2, `fa-times`],
112
    [3, `fa-ban`],
113
  ])
114
115
  return statusCodes.get(statusCode) || null
116
}
117
118
export function translateGameResult(result) {
119
  const statusCodes = new Map([
120
    [`WON`, `Gewonnen`],
121
    [`EQUAL`, `Gelijkgespeeld`],
122
    [`LOST`, `Verloren`],
123
  ])
124
  return statusCodes.get(result) || null
125
}
126
127
export function capitalizeFirstLetter(string) {
128
  return string.charAt(0).toUpperCase() + string.slice(1)
129
}
130
131
export function groupByDate(data) {
132
  const groups = data.reduce((groups, object) => {
133
    const date = object.start.split(` `)[0]
134
    if (!groups[date]) {
135
      groups[date] = []
136
    }
137
    groups[date].push(object)
138
    return groups
139
  }, {})
140
141
  // Edit: to add it in the array format instead
142
  const groupArrays = Object.keys(groups).map((date) => {
143
    return {
144
      date,
145
      objects: groups[date],
146
    }
147
  })
148
149
  return groupArrays
150
}
151
152
export function groupByMonth(data) {
153
  const groups = data.reduce((groups, object) => {
154
    const date = new Date(object.timestamp * 1000)
155
    const month = date.toLocaleString(`nl-BE`, { month: `long` })
156
    if (!groups[month]) {
157
      groups[month] = []
158
    }
159
    groups[month].push(object)
160
    return groups
161
  }, {})
162
163
  const groupArrays = Object.keys(groups).map((date) => {
164
    return {
165
      date,
166
      objects: groups[date],
167
    }
168
  })
169
170
  return groupArrays
171
}
172
173
export function replaceAll(source: string, search: string, replacement: string) {
174
  return source.replace(new RegExp(search, `g`), replacement)
175
}
176
177
export function sortRankings(a: RankingDataTeamObject, b: RankingDataTeamObject) {
178
  // Rank lager: A stijgt in sortering.
179
  if (a.rank < b.rank) {
180
    return -1
181
  }
182
  if (a.rank > b.rank) {
183
    return 1
184
  }
185
  // Aantal overwinningen hoger: A stijgt in sortering.
186
  if (a.wins > b.wins) {
187
    return -1
188
  }
189
  if (a.wins < b.wins) {
190
    return 1
191
  }
192
  // Doelpuntensaldo beter: A stijgt in sortering.
193
  if (a.goalsScored - a.goalsConceded > b.goalsScored - b.goalsConceded) {
194
    return -1
195
  }
196
  if (a.goalsScored - a.goalsConceded < b.goalsScored - b.goalsConceded) {
197
    return 1
198
  }
199
  // Aantal gemaakte doelpunten hoger: A stijgt in sortering.
200
  if (a.goalsScored > b.goalsScored) {
201
    return -1
202
  }
203
  if (a.goalsScored < b.goalsScored) {
204
    return 1
205
  }
206
  // Aantal uitoverwinningen hoger: A stijgt in sortering.
207
  if (a.winsAway > b.winsAway) {
208
    return -1
209
  }
210
  if (a.winsAway < b.winsAway) {
211
    return 1
212
  }
213
  // Doelpuntensaldo op verplaatsing beter: A stijgt in sortering.
214
  if (a.goalsScoredAway - a.goalsConcededAway > b.goalsScoredAway - b.goalsConcededAway) {
215
    return -1
216
  }
217
  if (a.goalsScoredAway - a.goalsConcededAway < b.goalsScoredAway - b.goalsConcededAway) {
218
    return 1
219
  }
220
  // Aantal gemaakte doelpunten op verplaatsing hoger: A stijgt in sortering.
221
  if (a.goalsScoredAway > b.goalsScoredAway) {
222
    return -1
223
  }
224
  if (a.goalsScoredAway < b.goalsScoredAway) {
225
    return 1
226
  }
227
228
  return a.team?.club?.localName.localeCompare(b.team?.club?.localName)
229
}
230
231
export default {
232
  mapMatchStatus,
233
  mapDivision,
234
  formatDivision,
235
  truncate,
236
  mapPositionCode,
237
  getPositions,
238
  mapPsdStatus,
239
  mapPsdStatusShort,
240
  mapPsdStatusIcon,
241
  translateGameResult,
242
  capitalizeFirstLetter,
243
  groupByDate,
244
  groupByMonth,
245
  replaceAll,
246
  sortRankings,
247
}
248